//<%@ page pageEncoding="UTF-8"%>
/**
 * form 表单 
 * 验证配置,例:[
 * {"name":"userId","required":true,"tip":"必填数字"},
 * {"name":"age","format":"Number","tip":"必填数字","tipId":"ageTip"},..]
 */
function Validator(form, conf, interrupt){
	if(typeof(form)=='string'){
		this.form = document.getElementById(form);
	}
	this.conf = conf;
	this.interrupt = interrupt?interrupt:false;//碰到错误后提示并返回
	
}
Validator.formatRegExp = {
		"Number"	:/^(-?\d+)(\.\d+)?$/,
		"Integer"	:/^-?\d+$/,
		"PositiveInteger"	:/^\d+$/,
		"Decimal"	:/^(-?\d+)(\.\d+)?$/,
		"PositiveDecimal"	:/^(\d+)(\.\d+)?$/,
		"Email"		:/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/,
		"English"	:/^[A-Za-z]+$/,
		"Chinese"	:/^[u4e00-u9fa5]+$/,
		"Date"		:/\d{4}-\d{2}-\d{2}(\s+\d{2})?(:\d{2})?(:\d{2})?/
}

Validator.prototype.checkFormat = function(item, value){
	if(item.format){
		var formatReg = Validator.formatRegExp[item.format];
		if(typeof(formatReg)=='string'){
			formatReg = new RegExp(formatReg);
		}
		return formatReg.test(value);
	}
	if(item.regExp){
		var reg = item.regExp;
		if(typeof(reg)=='string'){
			reg = new RegExp(reg);
		}
		return reg.test(value);
	}
	
	return true;
}

Validator.prototype.compareTo = function(item, inputValue){
	var compareValue = item.compareTo;
	var compareInput = this.form[item.compareTo];
	if(compareInput){
		compareValue = compareInput.value;
	}
	if(!compareValue){
		return true;
	}
	
	var result = true;
	var type = item.type;
	if(type=='eq'){
		result = inputValue==compareValue;
	}else if(type=='ne'){
		result = inputValue!=compareValue;
	}else if(type=='gt'){
		result = inputValue>compareValue;
	}else if(type=='ge'){
		result = inputValue>=compareValue;
	}else if(type=='lt'){
		result = inputValue<compareValue;
	}else if(type=='le'){
		result = inputValue<=compareValue;
	}
	return result;
}

Validator.prototype.between = function(item, inputValue){
	if(!item.between){
		return true;
	}
	var arr = item.between;
	var v0 = arr[0];
	var v1 = arr[1];
	return inputValue>=v0 && inputValue<=v1;
}

Validator.prototype.In = function(item, inputValue){
	if(!item.In){
		return true;
	}
	var arr = item.In;
	for(var i=0; i<arr.length; i++){
		if(inputValue == arra[i]){
			return true;
		}
	}
	return false;
}

Validator.prototype._getInputVal = function(input){
	var value;
	if(input.length && !input.tagName){
		value = '';
		var first = true;
		for(var j=0;j<input.length;j++){
			//兼容ie6
			try{ 
				if(input[j].checked);
			}catch(e){
				continue;
			}
			
			if(input[j].checked){
				if(!first){
					value += ',';
				}
				first = false;
				value += input[j].value;
			}
		}
	}else{
		value = input.value;
	}
	return value;
}

Validator.prototype.handleError = function(item, input){
	alert(item.tip);
	if(input.select)input.select();
	//showTipDiv(inupt,item.tip);
}

Validator.prototype.checkRequired = function(item, inputValue){
	if(item.required && !inputValue){
		return false;
	}
	return true;
}

// when=""
Validator.prototype.checkWhen = function(item, input, inputValue){
	var js = item.when;
	if(js){
		var result = eval('(' + js + ')');
		return result;
	}
	return true;
}

Validator.prototype.checkRule = function(item, input, inputValue){
	var js = item.rule;
	if(js){
		var result = eval('(' + js + ')');
		return result;
	}
	return true;
}

Validator.prototype.validate = function(){
	for(var i=0; i<this.conf.length; i++){
		var item = this.conf[i];
		var value = null;
		var input = this.form[item.name];
		if(input){
			value = this._getInputVal(input);
		}else{
			if(console&&console.log){
				console.log('表单['+item.name+']不存在！');
			}
		}
		
		var ok = false;
		if(!value){
			ok = this.checkRequired(item, value);
		}else{
			ok = this.checkWhen(item, input, value) 
				&& this.checkFormat(item, value) 
				&& this.compareTo(item, value) 
				&& this.between(item, value)
				&& this.In(item, value)
				&& this.checkRule(item, value);
		}

		if(!ok){
			this.handleError(item, input);
			if(this.interrupt){
				return false;
			}
		}
		
	}
	return true;
}
